// -*- coding: utf-8 -*-
frappe.ui.form.on('Scrap Issue Item', {
    // 余料字段变更事件
    scrap_material: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        // 清空相关字段
        frappe.model.set_value(cdt, cdn, 'location', '');
        frappe.model.set_value(cdt, cdn, 'material_type', '');
        frappe.model.set_value(cdt, cdn, 'material_name', '');
        frappe.model.set_value(cdt, cdn, 'color_pattern', '');
        frappe.model.set_value(cdt, cdn, 'length', '');
        frappe.model.set_value(cdt, cdn, 'width', '');
        frappe.model.set_value(cdt, cdn, 'thickness', '');
        frappe.model.set_value(cdt, cdn, 'available_quantity', '');
        frappe.model.set_value(cdt, cdn, 'uom', '');
        
        if (row.scrap_material) {
            // 获取余料信息
            frappe.model.with_doc('Scrap Material', row.scrap_material, function() {
                let scrap_material = frappe.model.get_doc('Scrap Material', row.scrap_material);
                
                // 设置余料信息
                frappe.model.set_value(cdt, cdn, 'location', scrap_material.location);
                frappe.model.set_value(cdt, cdn, 'material_type', scrap_material.material_type);
                frappe.model.set_value(cdt, cdn, 'material_name', scrap_material.material_name);
                frappe.model.set_value(cdt, cdn, 'color_pattern', scrap_material.color_pattern);
                frappe.model.set_value(cdt, cdn, 'length', scrap_material.length);
                frappe.model.set_value(cdt, cdn, 'width', scrap_material.width);
                frappe.model.set_value(cdt, cdn, 'thickness', scrap_material.thickness);
                frappe.model.set_value(cdt, cdn, 'available_quantity', scrap_material.quantity);
                frappe.model.set_value(cdt, cdn, 'uom', scrap_material.uom);
            });
        }
    },
    
    // 库位字段变更事件
    location: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        if (!row.location) return;
        
        // 验证库位有效性
        frappe.call({
            method: 'frappe.client.get_value',
            args: {
                doctype: 'Scrap Location',
                filters: { name: row.location },
                fieldname: ['status', 'warehouse']
            },
            callback: function(r) {
                if (r.message) {
                    let location = r.message;
                    
                    // 检查库位状态
                    if (location.status !== '启用') {
                        frappe.msgprint({
                            title: '库位状态异常',
                            message: `库位 ${row.location} 状态为 ${location.status}，无法使用`,
                            indicator: 'red'
                        });
                        frappe.model.set_value(cdt, cdn, 'location', '');
                        return;
                    }
                    
                    // 检查库位是否属于当前仓库
                    if (frm.doc.warehouse && location.warehouse !== frm.doc.warehouse) {
                        frappe.msgprint({
                            title: '库位仓库不匹配',
                            message: `库位 ${row.location} 不属于仓库 ${frm.doc.warehouse}`,
                            indicator: 'red'
                        });
                        frappe.model.set_value(cdt, cdn, 'location', '');
                        return;
                    }
                    
                    // 检查余料是否在该库位
                    if (row.scrap_material) {
                        frappe.call({
                            method: 'frappe.client.get_value',
                            args: {
                                doctype: 'Scrap Material',
                                filters: { name: row.scrap_material },
                                fieldname: ['location']
                            },
                            callback: function(r) {
                                if (r.message && r.message.location !== row.location) {
                                    frappe.msgprint({
                                        title: '余料库位不匹配',
                                        message: `余料不在库位 ${row.location} 中`,
                                        indicator: 'red'
                                    });
                                    frappe.model.set_value(cdt, cdn, 'location', '');
                                }
                            }
                        });
                    }
                }
            }
        });
    },
    
    // 数量字段变更事件
    quantity: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        if (!row.quantity || row.quantity <= 0) {
            frappe.msgprint({
                title: '数量无效',
                message: '出库数量必须大于0',
                indicator: 'red'
            });
            frappe.model.set_value(cdt, cdn, 'quantity', 1);
            return;
        }
        
        // 检查数量是否超过可用数量
        if (row.available_quantity && row.quantity > row.available_quantity) {
            frappe.msgprint({
                title: '数量超限',
                message: `出库数量 ${row.quantity} 超过可用数量 ${row.available_quantity}`,
                indicator: 'red'
            });
            frappe.model.set_value(cdt, cdn, 'quantity', row.available_quantity);
        }
    }
});

// 库位过滤函数
function get_location_filter_for_issue(frm) {
    let filters = {
        status: '启用'
    };
    
    // 如果已选择仓库，则只显示该仓库下的库位
    if (frm.doc.warehouse) {
        filters['warehouse'] = frm.doc.warehouse;
    }
    
    return filters;
}